//
//  GiftDetailCollectionViewCell.m
//  GiftNote
//
//  Created by wsh on 15/8/17.
//  Copyright (c) 2015年 GN. All rights reserved.
//

#import "GiftDetailCollectionViewCell.h"
#import "Screen.h"
#import "UIImageView+WebCache.h"

@implementation GiftDetailCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        _mainImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, (SCREEN_WIDTH - 20) / 2.0, (SCREEN_WIDTH - 20) / 2.0)];
        _mainImageView.layer.masksToBounds = YES;
        _mainImageView.layer.cornerRadius = 5;
        
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_mainImageView.frame) + 5, (SCREEN_WIDTH - 20) / 2.0, 30)];
        
        _picView = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_nameLabel.frame) + 7 + 20, 20, 20)];
        _picView.image = [UIImage imageNamed:@"iconfont-renminbi-3"];
        
        _priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(_nameLabel.frame)+ 20, (SCREEN_WIDTH - 20) / 2.0, 30)];
        _loveButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _loveButton.frame = CGRectMake((SCREEN_WIDTH - 20) / 2.0 - 60, CGRectGetMaxY(_nameLabel.frame) + 7 + 20, 15, 15);
        [_loveButton setImage:[UIImage imageNamed:@"iconfont-xihuan.png"] forState:UIControlStateNormal];
        
        _countLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_loveButton.frame) + 5, CGRectGetMaxY(_nameLabel.frame) + 20, 60, 30)];
        
        [self.contentView addSubview:_picView];
        [self.contentView addSubview:_mainImageView];
        [self.contentView addSubview:_nameLabel];
        [self.contentView addSubview:_priceLabel];
        [self.contentView addSubview:_loveButton];
        [self.contentView addSubview:_countLabel];
        
    }
    return self;
}



-(void)setModel:(GiftDetailModel *)model{

    _model = model;
    [_mainImageView sd_setImageWithURL:[NSURL URLWithString:model.cover_image_url]];
    _nameLabel.text = model.name;
    _priceLabel.text = model.price;
    _countLabel.text = [model.favorites_count stringValue];
    
    _nameLabel.numberOfLines = 2;
    _nameLabel.font = [UIFont systemFontOfSize:16];
    _nameLabel.frame = CGRectMake(_nameLabel.frame.origin.x, _nameLabel.frame.origin.y, _nameLabel.frame.size.width, [GiftDetailCollectionViewCell heightOfString:_nameLabel.text]);
    
}


+ (CGFloat)heightOfString:(NSString *)string{
    
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:16] forKey:NSFontAttributeName];
    CGRect bounds = [string boundingRectWithSize:CGSizeMake((SCREEN_WIDTH - 20) / 2.0, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return bounds.size.height;
}



@end
